<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class CountController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");
        
        public function index(){
          
	    header('Content-Type:text/html;charset=utf-8');
            $list_address= M('shop_address')->where('level=1')->select(); 
            
            if(!empty(I('select_name5'))){
		   $where['shop_prv']=I('select_name5');
	     }
	    if(!empty(I('select_name6'))){
		   $where['shop_city']=I('select_name6');
	    }
	    if(!empty(I('select_name7'))){
		   $where['shop_school']=I('select_name7');
	    }
		
	    $sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
		        $map['shop_name'] = array('LIKE','%'.$sh_q.'%');
			$map['id'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}
		
	       if(!empty($shArray)){$this->assign('shArray',$shArray);}
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
	        $list = M('shop')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select(); 
                
                //此时时间戳
	        $now_time = time(); 
                //当天凌晨
                $day_break = strtotime(date('Y-m-d'));  
                //本月第一天时间戳
                $month_first_day = mktime(0,0,0,date('m'),1,date('Y')); 
                //本年第一天时间戳
                $year_first_day = mktime(0,0,0,1,1,date("Y",time())); 
                
                
		foreach($list as $key=>$item){
		   
	    	$list[$key]['provin_name'] = M('shop_address')->where('id="'.$item['shop_prv'].'"')->getField('name');
		    $list[$key]['city_name'] = M('shop_address')->where('id="'.$item['shop_city'].'"')->getField('name');
		      $list[$key]['school_name'] = M('shop_address')->where('id="'.$item['shop_school'].'"')->getField('name');
                         $list[$key]['day_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$day_break .'" AND add_time <="'.$now_time .'"')->count();
                             $list[$key]['day_achievement'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$day_break .'" AND add_time <="'.$now_time .'"')->sum('total_cost');
				  $list[$key]['month_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->count();
                                        $list[$key]['month_achievement'] =  M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->sum('total_cost');
					    $list[$key]['year_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$year_first_day .'" AND add_time <="'.$now_time .'"')->count();  
                                               $list[$key]['year_achievement'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$year_first_day .'" AND add_time <="'.$now_time .'"')->sum('total_cost');
		}
		$this->assign('list',$list);
		$count = M('shop')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);// 状态
		$this->display('Count/index');
    }
        public  function disable(){
	    $buss_status = I('buss_status');
	    $id = I('id');
		if(!empty( $buss_status ) && !empty( $id )){
		 $shop_id = M('shop')->where('id="'.$id.'"')->getField('shop_id'); 
		   if( $buss_status == 1){
		      $data['buss_status'] = 2;
		      $resultA =  M('shop_detail')->where('shop_id="'.$shop_id.'"')->save($data);
		   } else{
		   
		      $data['buss_status'] = 1;
		      $resultB =  M('shop_detail')->where('shop_id="'.$shop_id.'"')->save($data);
		   
		   }
		   
		   
		   if($resultA || $resultB){
            
			$this->redirect(U('shopMange/index'));

		   }
		}
	
	
	
	  }
	
	public function editPoint(){
		header('Content-Type:text/html;charset=utf-8');
		
		
		$id = I('id',0,'int');
		$dataInfo = M('shop')->find($id);
		$dataInfo['keeper'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('keeper');
		$dataInfo['phone'] = M('user')->where('user_id="'.$dataInfo['shop_id'].'"')->getField('phone');
		$dataInfo['idcard'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('idcard');
		$dataInfo['bankcard'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bankcard');
		$dataInfo['bank'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bank');
		$dataInfo['build_time'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('build_time');
		$dataInfo['provin'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('provin');
		$dataInfo['city'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('city');
		$dataInfo['point'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('point');
		$dataInfo['bank_branch'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bank_branch');
		$dataInfo['buss_status'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('buss_status');
		$this->assign('dataInfo', $dataInfo);
	
		$this->display('Shop/edit');
    }	
	
	
	 public function checkStatus(){
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $data['check_status'] = 1;
	     $result = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->save($data);
	     if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	 }
	 
	 
	   public function savePoint(){
	     $cid = I('cid');
		 $data['point'] =  I('kd');
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $result = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->save($data);
		 if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	   }
           
           
           

        public function out() {
		$table="shop";  //数据表的名字
           $range = array ( //数据表的字段
                0 => 'id',
                1 => 'shop_name',
                2 => 'provin_name',
                3 => 'city_name',
                4 => 'school_name',
                5 => 'add_time',
                6 => 'day_order_num',
                7 => 'day_achievement',
                8 => 'month_order_num',
                9 => 'month_achievement',
                10 => 'year_order_num',
                11 => 'year_achievement',
        );
           $from_time = I('from_time','');
	   $to_time = I('to_time','');
           
           if($from_time){
			$from_time_s = strtotime($from_time);
			$where['add_time']= array('EGT',$from_time_s);
			
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['add_time']= array('ELT',$to_time_s);
			
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
                
        // 获得中文字段
        foreach ( $range as $k ) {  //将字段的名字 变成中文的
            $field [] = $this->setJisansuo ( $k );
        }
        $this->exportExcel ( $table, $field, $range,$where); //导出操作
	}


	/**
     * 格式化数据检索
     */
    public function setJisansuo($str){
        switch ($str) {
            case id:$file="商铺号";break;
            case shop_name:$file="商铺名";break;
            case provin_name:$file="所在省";break;
            case city_name:$file="所在市";break;
            case school_name:$file="大学";break;
            case add_time:$file="注册时间";break;
            case day_order_num:$file="本日订单数量";break;
            case day_achievement:$file="本日订单金额";break;
            case month_order_num:$file="本月订单数量";break;
            case month_achievement:$file="本月订单金额";break;
            case year_order_num:$file="本年订单数量";break;
            case year_achievement:$file="本年订单金额";break;
            default:$file="未知字段";break;
        }
        return $file;
    }

/**
     * 导出表格
     * @param unknown $table
     * @param unknown $filed
     * @param unknown $f1
     * @param string $where
     */
    public function exportExcel($table,$filed="",$f1="",$where=""){
        //加载需要的文件
        vendor ( "PHPExcel176.PHPExcel" );
        vendor ( 'PHPExcel176.PHPExcel.Writer.IWriter' );
        vendor ( 'PHPExcel176.PHPExcel.Writer.Excel5' );
        vendor ( 'PHPExcel176.PHPExcel' );
        vendor ( 'PHPExcel176.PHPExcel.IOFactory' );
        
        //此时时间戳
        $now_time = time(); 
        //当天凌晨
        $day_break = strtotime(date('Y-m-d'));  
        //本月第一天时间戳
        $month_first_day = mktime(0,0,0,date('m'),1,date('Y')); 
        //本年第一天时间戳
         $year_first_day = mktime(0,0,0,1,1,date("Y",time())); 
                
        //实例化数据表
        $model = D($table);
        //查询数据，讲数据封装到数组中
        $OrdersData = $model->where($where)->select(); // 查询数据得到$OrdersData二维数组
        foreach ( $OrdersData as $key => $item){
            $OrdersData[$key]['provin_name'] = M('shop_address')->where('id="'.$item['shop_prv'].'"')->getField('name');
               $OrdersData[$key]['city_name'] = M('shop_address')->where('id="'.$item['shop_city'].'"')->getField('name');
		      $OrdersData[$key]['school_name'] = M('shop_address')->where('id="'.$item['shop_school'].'"')->getField('name');
                         $OrdersData[$key]['day_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$day_break .'" AND add_time <="'.$now_time .'"')->count();
                             $OrdersData[$key]['day_achievement'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$day_break .'" AND add_time <="'.$now_time .'"')->sum('total_cost');
				  $OrdersData[$key]['month_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->count();
                                        $OrdersData[$key]['month_achievement'] =  M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->sum('total_cost');
					    $OrdersData[$key]['year_order_num'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$year_first_day .'" AND add_time <="'.$now_time .'"')->count();  
                                               $OrdersData[$key]['year_achievement'] = M('shop_product_order')->where('from_mid="'.$item['mid'].'" AND order_status = 8 AND add_time >="'.$year_first_day .'" AND add_time <="'.$now_time .'"')->sum('total_cost');

            $OrdersData[$key]['add_time'] = date("Y-m-d H:i",$item['add_time']); 
          
        }
        
        $objPHPExcel = new \PHPExcel ();//实例化对象
        $objPHPExcel->getProperties ()->setCreator ( "ctos" )->setLastModifiedBy ( "ctos" )->setTitle ( "Office 2007 XLSX Test Document" )->setSubject ( "Office 2007 XLSX Test Document" )->
        setDescription ( "Test document for Office 2007 XLSX, generated using PHP classes." )->setKeywords ( "office 2007 openxml php" )->setCategory ( "Test result file" );
        $objPHPExcel->getActiveSheet ()->getRowDimension ( '1' )->setRowHeight ( 22 );
        $objPHPExcel->getActiveSheet ()->getRowDimension ( '2' )->setRowHeight ( 20 );
        $objPHPExcel->getActiveSheet ()->getRowDimension ( '1' )->setRowHeight ( 22 );
        $objPHPExcel->getActiveSheet ()->getRowDimension ( '2' )->setRowHeight ( 20 );
        $objPHPExcel->getActiveSheet ()->mergeCells ( 'A1:J1' );
        $count=count($filed);
        $objPHPExcel->setActiveSheetIndex ( 0 )->
        setCellValue ( 'A1', '学生惠统计管理:' . '创建时间：' . date ( 'Y-m-d H:i:s' ) . '操作人：' . 'administrator' );
        /*
         * 设置表格的第一行的标题
         */
        for ($i=0;$i<=$count;$i++){
            $objPHPExcel->setActiveSheetIndex ( 0 )->setCellValue ( chr($i+65).'2', $filed[$i] );
        }
        /*
         *循环的吧数据插入表中
         *循环数据，将数据插入表格中
        */
        for($i = 0; $i < count ( $OrdersData ) ; $i ++){
            for ($j=0;$j<=$count;$j++){
                $v=$f1[$j];
                $objPHPExcel->getActiveSheet ( 0 )->setCellValue ( chr($j+65) . ($i + 3), $OrdersData [$i][$v] );
            }
        }
        $objPHPExcel->getActiveSheet ()->setTitle ( '学生惠统计管理' );
        $objPHPExcel->setActiveSheetIndex ( 0 );
        header ( 'Content-Type: application/vnd.ms-excel' );
        header ( 'Content-Disposition: attachment;filename="统计管理(' . date ( 'Ymd-His' ) . ').xls"' ); // 日期为文件名后缀
        header ( 'Cache-Control: max-age=0' );
        $objWriter = \PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' ); // excel5为xls格式，excel2007为xlsx格式
        $objWriter->save ( 'php://output' );
    }
	
           
           
           
}